﻿#pragma once
#include "datetime.h"
using namespace MySpace;

bool Date::operator<(const Date& d) const
{
	if (_year < d._year)
	{
		return true;
	}
	else if (_year == d._year)
	{
		if (_month < d._month)
		{
			return true;
		}
		else if (_month == d._month && _day < d._day)
		{
			return true;
		}
	}
	return false;
}

bool Date::operator<=(const Date& d) const
{
	return *this < d || *this == d;
}

bool Date::operator>(const Date& d) const
{
	return !(*this < d || *this == d);
}

bool Date::operator>=(const Date& d) const
{
	return !(*this < d);
}

bool Date::operator==(const Date& d) const
{
	if ((_year == d._year) && (_month == d._month) && (_day == d._day))
	{
		return true;
	}
	return false;
}

bool Date::operator!=(const Date& d) const
{
	return !(*this == d);
}


// d1 += 天数
Date& Date:: operator+=(int day)
{
	if (day < 0)
	{
		int temp = -day;
		SubDay(day);
	}
	else
	{
		AddDay(day);
	}
	return *this;
}
Date Date:: operator+(int day) const
{
	Date copyDate = *this;
	copyDate += day;
	return copyDate;
}
// d1 -= 天数
Date& Date:: operator-=(int day)
{
	if (day > 0)
	{
		int temp = -day;
		SubDay(day);
	}
	else
	{
		AddDay(day);
	}
	return *this;
}
Date Date:: operator-(int day) const
{
	Date copyDate = *this;
	copyDate -= day;
	return copyDate;
}
// d1 - d2
int Date:: operator-(const Date& d) const
{
	Date maxdate(*this);
	Date mindate(d);
	int record = 0;

	if (maxdate < mindate)
	{
		Date temp;
		temp = maxdate;
		maxdate = mindate;
		mindate = temp;
	}
	
	while (maxdate > mindate)
	{
		if ((maxdate._year == mindate._year) && (maxdate._month == mindate._month))
		{
			int day = maxdate._day - mindate._day;
			record += day;
			maxdate -= day;
		}
		else
		{
			record += maxdate._day;
			maxdate -= maxdate._day;
		}
	}
	return record;
}

// ++d1 -> d1.operator++()
Date& Date:: operator++()
{
	AddDay(1);
	return *this;
}
// d1++ -> d1.operator++(0)
// 为了区分，构成重载，给后置++，强⾏增加了⼀个int形参
// 这⾥不需要写形参名，因为接收值是多少不重要，也不需要⽤
// 这个参数仅仅是为了跟前置++构成重载区分
Date Date:: operator++(int)
{
	Date copydate = (*this);
	AddDay(1);
	return copydate;
}
Date& Date::operator--()
{
	SubDay(1);
	return *this;
}
Date Date:: operator--(int)
{
	Date copydate = (*this);
	SubDay(1);
	return copydate;
}

//重载输出函数
ostream& operator<<(ostream& out,const Date& date)
{
	out << date.GetYear() << "年" << date.GetMonth() << "月" << date.GetDay() << "日" << endl;
	return out;
}

//重载输入函数
istream& operator>>(istream& in, Date& date)
{
	int year = 1970, month = 1, day = 1;
	in >> year >> month >> day;
	date.SetYear(year);
	date.SetMonth(month);
	date.SetDay(day);
	return in;
}